Arrays are one-based, random access collections of objects. To create an array variable, declare:
var a := {}; // empty array
var b := { 5, 32, "hello", "world" }; // initialized array
var c := { {1,2}, {3,4,5} }; // array containing arrays
|
An array can be assigned to any other variable, even if that variable was not declared an array. Therefore:
var a := { 2, 4, 6, 8 };
var b;
b := a;
|
is legal.
Similarly, if a function returns an array, no special declaration is needed:
var a; a := function_that_returns_an_array(); |
Arrays grow automatically:
var a := {};
a[1] := 4;
a[4] := 7;
|
Arrays elements can be any type of object, including another array:
Local a := {};
Local b := {};
a[1] := 5;
b[1] := a;
b[2] := 6;
|
The following are equivalent methods of looping through an array. The 'foreach' method is much more efficient, as well as being more convenient.
Local a := { 2,4,6,8 };
Local i;
for( i := 1; i <= len(a); i := i + 1 )
print( a[i] );
endfor
foreach i in a
print( i );
endforeach
|